﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace TimeTableManagement.Models
{
    public class Functions
    {
        /// <summary>
        /// Ham ma hoa MD5
        /// </summary>
        /// <param name="input">Chuoi can ma hoa</param>
        /// <returns>Chuoi ket qua MD5</returns>
        public static string Encrypt(string input) 
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bs = Encoding.UTF8.GetBytes(input);
            bs = md5.ComputeHash(bs);
            StringBuilder s = new StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            return s.ToString();
        }
        /// <summary>
        /// Tao chuoi password ngau nhien
        /// </summary>
        /// <param name="len">Do dai chuoi can tao</param>
        /// <returns>String ngau nhien duoc tao</returns>
        public static string RandomPassword(int len)
        {
           
            Random ran = new Random(DateTime.Now.Second);
            char[] password = new char[len];
            for (int i = 0; i < len; i++)
            {
                int[] n = { ran.Next(48, 57), ran.Next(65, 90), ran.Next(97, 122) };
                int picker = ran.Next(0, 3);
                if (picker == 3)
                    picker = 2;
                password[i] = (char)n[picker];
            }
            return new string(password);
        }
        public static String Truncate(string input, int length)
        {
            if (input.Length <= length)
            {
                return input;
            }
            else
            {
                return input.Substring(0, length) + "...";
            }
        }
    }
    
}